package com.css.zfzx.fxpg.modules.preestimate.workspace.repository.impl;

import com.css.zfzx.fxpg.modules.preestimate.workspace.repository.FxPreestimateWorkspaceNativeRepository;
import com.css.zfzx.fxpg.modules.preestimate.workspace.repository.entity.FxPreestimateWorkspaceEntity;
import com.css.zfzx.fxpg.modules.preestimate.workspace.viewobjects.FxPreestimateWorkspaceQueryParams;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;
/**
 * @author andy
 * @date 2020-11-19
 */
@Repository
public class FxPreestimateWorkspaceNativeRepositoryImpl implements FxPreestimateWorkspaceNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxPreestimateWorkspaceNativeRepositoryImpl.class);


    @Override
    public Page<FxPreestimateWorkspaceEntity> queryFxPreestimateWorkspaces(FxPreestimateWorkspaceQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_preestimate_workspace ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getTime())) {
            whereSql.append(" and time_ = :time");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxPreestimateWorkspaceEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_preestimate_workspace ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getTime())) {
            query.setParameter("time", queryParams.getTime());
            countQuery.setParameter("time", queryParams.getTime());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxPreestimateWorkspaceEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxPreestimateWorkspaceEntity> fxPreestimateWorkspacePage = new PageImpl<>(list, pageable, count.longValue());
        return fxPreestimateWorkspacePage;
    }

    @Override
    public List<FxPreestimateWorkspaceEntity> listFxPreestimateWorkspaces(FxPreestimateWorkspaceQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_preestimate_workspace ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getTime())) {
            whereSql.append(" and time_ = :time");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getOBJECTID())) {
            whereSql.append(" and OBJECTID = :OBJECTID");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxPreestimateWorkspaceEntity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getTime())) {
            query.setParameter("time", queryParams.getTime());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getOBJECTID())) {
            query.setParameter("OBJECTID", queryParams.getOBJECTID());
        }
        List<FxPreestimateWorkspaceEntity> list = query.getResultList();
        return list;
    }

}
